#include "timer_device.h"
#include "my_errno.h"
#include "tim.h"
#include <string.h>


static int TimerInit(struct Timer_Device *ptDev)
{
	if (NULL == ptDev)
		return -EINVAL;

	PTimerData ptPrivData = (TimerData *)ptDev->privData;
	HAL_TIM_Base_Start_IT(ptPrivData->handle);

	return ESUCCESS;
}

/*
static void TimerCallback(struct Timer_Device *ptDev)
{

}
*/

//----------------------------------------
static TimerData g_tTimer6Data ={ &htim6 };
static TimerDevice g_tTimer6Device = {
	.name = "timer6",
	.Init = TimerInit,
	.Callback = NULL,
	.privData = &g_tTimer6Data
};

static TimerData g_tTimer2Data ={ &htim2 };
static TimerDevice g_tTimer2Device = {
	.name = "timer2",
	.Init = TimerInit,
	.Callback = NULL,
	.privData = &g_tTimer2Data
};

static PTimerDevice g_ptTimerDevices[] = {
	&g_tTimer6Device, &g_tTimer2Device
};

//----------------------------------------
PTimerDevice GetTimerDevice(char *name)
{
	for (int i = 0; i < sizeof(g_ptTimerDevices) / sizeof(g_ptTimerDevices[0]); i++)
	{
		if (!strcmp(g_ptTimerDevices[i]->name, name))
			return g_ptTimerDevices[i];
	}

	return NULL;
}


void TimerPeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (TIM6 == htim->Instance)
	{
		if (g_tTimer6Device.Callback)
			g_tTimer6Device.Callback();
	}
	else if (TIM2 == htim->Instance)
	{
		if (g_tTimer2Device.Callback)
			g_tTimer2Device.Callback();
	}
}



